package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.bo.GoodsBO;
import com.junzhiit.nengji.bo.ModulesGoodsBO;
import com.junzhiit.nengji.bo.ModulesGoodsListBO;
import com.junzhiit.nengji.cms.service.ModulesGoodsService;
import com.junzhiit.nengji.cms.web.exception.ModulesGoodsIsExistException;
import com.junzhiit.nengji.cms.web.exception.ModulesGoodsNotFoundException;
import com.junzhiit.nengji.cms.web.exception.ModulesGoodsNotNextException;
import com.junzhiit.nengji.cms.web.exception.ModulesGoodsNotPrevException;
import com.junzhiit.nengji.cms.web.exception.ModulesNotFoundException;
import com.junzhiit.nengji.cms.web.util.Constants;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.GoodsListVO;
import com.junzhiit.nengji.cms.web.vo.ModulesGoodsAddVO;
import com.junzhiit.nengji.cms.web.vo.ModulesGoodsListVO;
import com.junzhiit.nengji.cms.web.vo.ModulesGoodsSortVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.GoodsDAO;
import com.junzhiit.nengji.dao.ModulesDAO;
import com.junzhiit.nengji.dao.ModulesGoodsDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Goods;
import com.junzhiit.nengji.model.Modules;
import com.junzhiit.nengji.model.ModulesGoods;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.enums.GoodsStatus;
import com.junzhiit.nengji.model.enums.Is;
import com.junzhiit.nengji.model.enums.ModulesGoodsStatus;

@Service
public class ModulesGoodsServiceImpl extends BaseServiceImpl implements ModulesGoodsService {

	@Autowired
	private ModulesGoodsDAO modulesGoodsDAO;

	@Autowired
	private ModulesDAO modulesDAO;
	
	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;
	
	@Autowired
	private GoodsDAO goodsDAO;

	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, ModulesGoodsListVO modulesGoodsListVO) {
		ModulesGoodsListBO modulesGoodsListBO = new ModulesGoodsListBO();
		BeanUtils.copyProperties(modulesGoodsListVO, modulesGoodsListBO);

		List<ModulesGoodsBO> list = modulesGoodsDAO.findAll(modulesGoodsListBO, dtp.getStart(), dtp.getLength());
		int totalCount = modulesGoodsDAO.getTotalCountByAll(modulesGoodsListBO);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO getTotalCount(String id) {
		ResultVO result = new ResultVO();
		int count = modulesGoodsDAO.getTotalCount(id);
		result.setData(count);
		return result;
	}

	@Override
	public ResultVO add(ModulesGoodsAddVO modulesGoodsAddVO) {
		ResultVO result = new ResultVO();
		ModulesGoods modulesGoods = new ModulesGoods();

		int count = modulesGoodsDAO.queryGoodsIsExist(modulesGoodsAddVO.getGoodsId(), modulesGoodsAddVO.getModulesId());
		if (count > 0) {
			throw new ModulesGoodsIsExistException("模块商品已存在");
		}

		BeanUtils.copyProperties(modulesGoodsAddVO, modulesGoods);
		if(modulesGoodsAddVO.getStatus()==null){
			modulesGoods.setStatus(ModulesGoodsStatus.INACTIVE.toIntStatus());
		}
		Integer Maxsort = modulesGoodsDAO.getMaxSort(modulesGoodsAddVO.getModulesId());
		modulesGoods.setSort(Maxsort + 1);
		ModulesGoods data = modulesGoodsDAO.insert(modulesGoods);
		
		//同步更新商品详情
		int goodsCount=modulesGoodsDAO.getTotalCountByGoodsId(modulesGoodsAddVO.getGoodsId());
		Goods goods=new Goods();
		goods.setId(modulesGoodsAddVO.getGoodsId());
		goods.setIsSynchronous(Is.YES.toIntValue());
		goodsDAO.update(goods);
		
		result.setData(data);
		return result;
	}

	@Override
	public Map<String, Object> findModulesAll(DataTablesParamVO dtp, GoodsListVO goodsListVO,String sessionId) {

		String modulesId = goodsListVO.getModulesId();
		Modules modules = modulesDAO.query(modulesId);
		if (modules == null) {
			throw new ModulesNotFoundException("模块不存在");
		}
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		Shop shop = null;
		if(StringUtils.isNotBlank(systemUserSessionVO.getSystemId())){
			shop = shopDAO.query(systemUserSessionVO.getSystemId());
		}
		
		Goods goods = new Goods();
		int isRecommandGoods = modules.getIsRecommandGoods();
		if (isRecommandGoods == 1) {
			goods.setIsRecommend(isRecommandGoods);
		}
		
		goodsListVO.setStatus(GoodsStatus.ACTIVE.toIntStatus());
		BeanUtils.copyProperties(goodsListVO, goods);
		
		if (shop != null) {
			goods.setShopId(shop.getId());
		}
		
		List<GoodsBO> list = modulesGoodsDAO.findGoodsPageAll(goods, goodsListVO.getModulesId(), dtp.getStart(),
				dtp.getLength());
		int totalCount = modulesGoodsDAO.getGoodsTotalCount(goods, goodsListVO.getModulesId());
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO del(String id) {
		ResultVO result = new ResultVO();
		ModulesGoods modulesGoods = modulesGoodsDAO.query(id);
		if (modulesGoods == null) {
			throw new ModulesGoodsNotFoundException("模块商品未找到");
		}
		boolean data = modulesGoodsDAO.delete(id);
		result.setData(data);
		
		//移除同步商品详情
		int count=modulesGoodsDAO.getTotalCountByGoodsId(id);
		if(count==Constants.ZERO){
			Goods goods=new Goods();
			goods.setId(modulesGoods.getGoodsId());
			goods.setIsSynchronous(Is.NO.toIntValue());
			goodsDAO.update(goods);
		}
		
		return result;
	}

	@Override
	public ResultVO sortDown(ModulesGoodsSortVO modulesGoodsSortVO) {
		ResultVO result = new ResultVO();
		ModulesGoods modulesGoods = modulesGoodsDAO.query(modulesGoodsSortVO.getId());
		if (modulesGoods == null) {
			throw new ModulesGoodsNotFoundException("模块商品未找到");
		}
		ModulesGoods params = new ModulesGoods();
		BeanUtils.copyProperties(modulesGoodsSortVO, params);
		ModulesGoods prevModulesGoods = modulesGoodsDAO.getPreModulesGoods(params);
		if (prevModulesGoods == null) {
			throw new ModulesGoodsNotPrevException("已到底");
		}

		Integer preSort = prevModulesGoods.getSort();
		Integer sort = modulesGoods.getSort();

		prevModulesGoods.setSort(sort);
		modulesGoods.setSort(preSort);
		modulesGoodsDAO.update(prevModulesGoods);
		modulesGoodsDAO.update(modulesGoods);

		return result;
	}

	@Override
	public ResultVO sortUp(ModulesGoodsSortVO modulesGoodsSortVO) {
		ResultVO result = new ResultVO();
		ModulesGoods modulesGoods = modulesGoodsDAO.query(modulesGoodsSortVO.getId());
		if (modulesGoods == null) {
			throw new ModulesGoodsNotFoundException("模块商品未找到");
		}
		ModulesGoods params = new ModulesGoods();
		BeanUtils.copyProperties(modulesGoodsSortVO, params);
		ModulesGoods nextModulesGoods = modulesGoodsDAO.getNextModulesGoods(params);
		if (nextModulesGoods == null) {
			throw new ModulesGoodsNotNextException("已到顶");
		}
		Integer nextSort = nextModulesGoods.getSort();
		Integer sort = modulesGoods.getSort();
		nextModulesGoods.setSort(sort);
		modulesGoods.setSort(nextSort);
		modulesGoodsDAO.update(nextModulesGoods);
		modulesGoodsDAO.update(modulesGoods);
		return result;
	}

	@Override
	public ResultVO sortStick(ModulesGoodsSortVO modulesGoodsSortVO) {
		ResultVO result = new ResultVO();
		ModulesGoods modulesGoods = modulesGoodsDAO.query(modulesGoodsSortVO.getId());
		if (modulesGoods == null) {
			throw new ModulesGoodsNotFoundException("模块商品未找到");
		}
		Integer Maxsort = modulesGoodsDAO.getMaxSort(modulesGoodsSortVO.getModulesId());
		if (modulesGoods.getSort() == Maxsort) {
			throw new ModulesGoodsNotNextException("已到顶");
		}
		modulesGoods.setSort(Maxsort + 1);
		boolean data = modulesGoodsDAO.update(modulesGoods);
		result.setData(data);
		return result;
	}

	@Override
	public ResultVO updateStatus(String id) {
		ResultVO result = new ResultVO();
		ModulesGoods modulesGoods = modulesGoodsDAO.query(id);
		if (modulesGoods == null) {
			throw new ModulesGoodsNotFoundException("模块商品未找到");
		}

		if (modulesGoods.getStatus() == ModulesGoodsStatus.INACTIVE.toIntStatus()) {
			modulesGoods.setStatus(ModulesGoodsStatus.ACTIVE.toIntStatus());
		} else {
			modulesGoods.setStatus(ModulesGoodsStatus.INACTIVE.toIntStatus());
		}
		boolean data = modulesGoodsDAO.update(modulesGoods);
		result.setData(data);
		return result;
	}

	@Override
	public ResultVO batchUpdateStatus(String[] ids, int status) {
		ResultVO result = new ResultVO();
		if (status == ModulesGoodsStatus.ACTIVE.toIntStatus() || status == ModulesGoodsStatus.INACTIVE.toIntStatus()) {
			boolean data = modulesGoodsDAO.batchUpdateStatus(ids, status);
			result.setData(data);
		}
		return result;

	}
}
